import { glConfig } from '../../Config/GLConfig';
import TableItemPrefab from '../../Hall/FPJTableView/TableItemPrefab';
import { glUtils } from '../../lib/GLUtils';
import { UIMgr } from '../../lib/UIMgr';
import { GCtrl } from '../../lib/util/GCtrl';
import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";



const { ccclass, property } = cc._decorator;

@ccclass
export default class HallUI extends cc.Component {

	@property(cc.Sprite)
	sp_head: cc.Sprite = null;

	@property(cc.Sprite)
	headRim: cc.Sprite = null;

	@property(cc.Label)
	nikeName: cc.Label = null;

	@property(cc.Label)
	lbl_rid: cc.Label = null;

	@property(cc.Label)
	lbl_gold: cc.Label = null;


	@property(cc.Node)
	BtnManagerNode: cc.Node = null;

	@property(cc.Node)
	refresh: cc.Node = null;

	_fpjtable: cc.Node = null;
	//
	upAct = null;
	downAct = null;
	leftAct = null;
	rightAct = null;
	refreshTime = 0;
	isClickRefresh = false;
	eventCustomUpdatePlayerInfo = null;
	eventCustomNetSuccess = null;
	eventupdateFeedbackRedPoit = null;
	eventrefreshMail = null;
	eventChooseGame = null;
	eventRoomBack = null;
	eventCustomPhoneBindEnd = null;
	eventrefreshmailgetall = null;
	eventrefreshmailopen = null;
	eventChangeHeadEnd = null;

	eventCustomVipLevelChange = null;
	/** 昵称修改成功事件 */
	eventChangeNickSuccess = null;

	//红包大小
	private hongbaoCoin: number = null;
	//红包兑换线
	private readonly hongbaoDuihuanLine: number = 1000;

	private eventIndexList: any = {};



	onLoad() {
		if (!cc.vv.session.getPhone()) {
			//	this.gift.active = true;
		} else {
			//this.gift.active = false;
		}

		let dt = 0.5;
		this.upAct = cc.moveBy(dt, cc.v2(0, 150)).easing(cc.easeCircleActionInOut());
		this.downAct = cc.moveBy(dt, cc.v2(0, -150)).easing(cc.easeCircleActionInOut());
		this.leftAct = cc.moveBy(dt, cc.v2(-500, 0)).easing(cc.easeCircleActionInOut());
		this.rightAct = cc.moveBy(dt, cc.v2(500, 0)).easing(cc.easeCircleActionInOut());
		this.updatePlayerInfo();
		this.refreshTime = 1;
		this.setTopUI();
		this.isClickRefresh = false;
		// this.show();
		this.refreshGlod();
		cc.game.on(
			cc.game.EVENT_SHOW,
			function () {
				console.log("切换前台 刷新金币");
				this.onClickRefresh()
			}.bind(this)
		)
	}

	start() {
		if (!this._fpjtable && !cc.isValid(this._fpjtable)) {
			cc.loader.loadRes(cc.vv.GLConfig.FPJ_TableView, cc.Prefab, (err, prefab) => {
				if (err) {
					cc.vv.GLUtils.showAlert1(cc.vv.GLConfig.FPJ_TableView + " prefab loading failed!");
				}
				if (!cc.isValid(parent)) {
					return;
				}
				if (prefab) {
					this._fpjtable = cc.instantiate(prefab);
					if (this._fpjtable) {
						this._fpjtable.parent = this.node;
					}
				} else {
					console.error(err);
				}
			})
		} else {

		}
	}

	onEnable() {
		this.eventCustomUpdatePlayerInfo = cc.vv.eventCustom.on("updatePlayerInfo", this.updatePlayerInfo.bind(this), this.node);
		this.eventCustomNetSuccess = cc.vv.eventCustom.on("netSuccess", this.netSuccess.bind(this), this.node);
		this.eventrefreshMail = cc.vv.eventCustom.on("mailadd", this.refreshRedPoint.bind(this), this.node);
		this.eventrefreshmailgetall = cc.vv.eventCustom.on("mailgetall", this.refreshRedPointNum.bind(this), this.node);
		this.eventrefreshmailopen = cc.vv.eventCustom.on("hallmailopen", this.refreshRedPointNum.bind(this), this.node);
		this.eventCustomPhoneBindEnd = cc.vv.eventCustom.on("phoneBindEnd", this.phoneBindEnd.bind(this), this.node);
		this.eventChangeHeadEnd = cc.vv.eventCustom.on("ChangeHeadEnd", this.changeHeadEnd.bind(this), this.node);
		this.eventCustomVipLevelChange = cc.vv.eventCustom.on("VipLevelChange", this.vipLevelChange.bind(this), this.node);
		this.eventIndexList["changeHeadSuccess"] = GCtrl.eventCustom.on("changeHeadSuccess", this.changeHeadSuccess.bind(this), this.node);
		this.eventChangeNickSuccess = GCtrl.eventCustom.on("changeNickSuccess", this.changeNickSuccess.bind(this), this.node);


		cc.vv.eventCustom.on("StartShowGame", this.StartShowGame.bind(this), this.node);
		cc.vv.eventCustom.on("ExitShowGame", this.ExitShowGame.bind(this), this.node);
		cc.vv.eventCustom.on("openHall", (args) => {
			//绑定手机
			if (!cc.vv.session.getPhone()) {
				cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_BindPhone_path);
			} else if (!cc.vv.session.getIsBangDingPw()) {
				cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_BindPasswrod_path);
			} else {
				this.checkParentRid();
			}

		}, this.node);
		cc.vv.audioUtil.playMusic("HallDWC/hall/music/bg");

		cc.vv.UIMgr.open(cc.vv.GLConfig.Dwc_SimpleChat_path,);
	}

	checkParentRid() {
		if (!cc.vv.session.getParentRid()) { 	// 没有直属代理 ,还要检查是否为最高代理
			cc.vv.session.getIsAgent((isAgent) => {
				if (!isAgent) {
					cc.vv.UIMgr.open(cc.vv.GLConfig.FPJ_BindShangJi);
				}
			}, () => {
				//这个时候断网，就没法获取，也默认打开界面，不允许操作
				cc.vv.UIMgr.open(cc.vv.GLConfig.FPJ_BindShangJi);
			})
		}
	}

	onDisable() {
		if (typeof this.eventCustomUpdatePlayerInfo != "undefined") {
			cc.vv.eventCustom.off("updatePlayerInfo", this.eventCustomUpdatePlayerInfo);
			this.eventCustomUpdatePlayerInfo = undefined;
		}

		if (typeof this.eventCustomNetSuccess != "undefined") {
			cc.vv.eventCustom.off("netSuccess", this.eventCustomNetSuccess);
			this.eventCustomNetSuccess = undefined;
		}

		// if (typeof this.eventupdateFeedbackRedPoit != "undefined") {
		// 	cc.vv.eventCustom.off("updateFeedbackRedPoit", this.eventupdateFeedbackRedPoit);
		// 	this.eventupdateFeedbackRedPoit = undefined;
		// }
		if (typeof this.eventrefreshMail != "undefined") {
			cc.vv.eventCustom.off("mailadd", this.refreshRedPoint);
			this.eventrefreshMail = undefined;
		}

		// if (typeof this.eventChooseGame != "undefined") {
		// 	cc.vv.eventCustom.off("chooseGame", this.eventChooseGame);
		// 	this.eventChooseGame = undefined;
		// }
		// if (typeof this.eventRoomBack != "undefined") {
		// 	cc.vv.eventCustom.off("roomBack", this.eventRoomBack);
		// 	this.eventRoomBack = undefined;
		// }
		if (typeof this.eventCustomPhoneBindEnd != "undefined") {
			cc.vv.eventCustom.off("phoneBindEnd", this.eventCustomPhoneBindEnd);
			this.eventCustomPhoneBindEnd = undefined;
		}
		if (typeof this.eventrefreshmailgetall != "undefined") {
			cc.vv.eventCustom.off("mailgetall", this.eventrefreshmailgetall);
			this.eventrefreshmailgetall = undefined;
		}
		if (typeof this.eventrefreshmailopen != "undefined") {
			cc.vv.eventCustom.off("hallmailopen", this.eventrefreshmailopen);
			this.eventrefreshmailopen = undefined;
		}

		if (typeof this.eventChangeHeadEnd != "undefined") {
			cc.vv.eventCustom.off("ChangeHeadEnd", this.eventChangeHeadEnd);
			this.eventChangeHeadEnd = undefined;
		}

		if (typeof this.eventCustomVipLevelChange != "undefined") {
			cc.vv.eventCustom.off("VipLevelChange", this.eventCustomVipLevelChange);
			this.eventCustomVipLevelChange = undefined;
		}


		//this.hide();
		this.eventIndexList["changeHeadSuccess"] && (GCtrl.eventCustom.off("changeHeadSuccess", this.eventIndexList["changeHeadSuccess"]), delete this.eventIndexList["changeHeadSuccess"]);

		cc.vv.UIMgr.close(cc.vv.GLConfig.Dwc_SimpleChat_path);
	}
	onDestroy() {
		//cc.vv.UIMgr.close(cc.vv.GLConfig.Dwc_SimpleChat_path);
	}
	//刷新用户数据;
	updatePlayerInfo() {
		var _player = cc.vv.session.getPlayer();
		this.setNick(_player.rolename || ``);
		this.lbl_rid.string = "ID:" + _player.rid;
		this.lbl_gold.string = "" + glUtils.getExchangeScore(_player.coin);
		glUtils.lableWidthLimit(this.lbl_gold, 186)
		GCtrl.setHeadSpr(_player.logo, this.sp_head);
	}
	/** 设置昵称 */
	private setNick(_nick: string): void {
		this.nikeName.string = GCtrl.visibleStr(_nick, 5);
	}
	private changeHeadSuccess(data) {
		let headId = data[0];
		if (!headId) return false;
		GCtrl.setHeadSpr(headId, this.sp_head);
		return true;
	}
	/** 昵称修改成功 */
	private changeNickSuccess(_evt): void {
		this.setNick(_evt[0] || ``);
	}
	update(dt) {

	}
	setTopUI() {
		//只有代理才能打开这个按钮
		cc.vv.session.getIsAgent((isAgent) => {
			this.BtnManagerNode.active = isAgent;
		})
	}

	setTopButton() {


	}

	updateFeedbackRedPoit(arg) {

	}

	//设置;
	onClickSetting() {
		UIMgr.open(glConfig.Hall_Setting_Path)
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
	}

	//游戏客服;
	onClickCustomerService() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
	}

	//支付点击;
	onClickShop() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_Shop_path);
	}

	//公告
	// onClickNotice () {
	//     //   cc.vv.GLPageMgr.openPage("Page_Mail");
	//     cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
	// },

	//战绩
	onClickRecord() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.Record_path);
	}

	//保险箱
	onClickBank() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_SafeBankView_path);
	}
	//消息
	onClickMessage() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_Message_path);

	}

	//洗码
	onClickCleanCode() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.cleanCode_path);
	}

	//你提我改
	onClickFeedback() {
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.Feedback_path, null, null, "反馈打开中。。。");
	}

	//提款
	onClickExchange() {

		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		let is_bind = cc.vv.GLUtils.checkPhoneBind();
		if (!is_bind) {
			// cc.vv.UIMgr.open(cc.vv.GLConfig.Gift_path);
			// cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
			cc.vv.UIMgr.open(cc.vv.GLConfig.IdentifyView_path);
			return;
		}
		//cc.vv.GLUtils.showTips("请联系客服或者您的代理上级");
		cc.vv.ExchangeDataModel.init();

	}

	//左上角头像点击;
	OnClickHead() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_PersonalInfo_path);
	}

	//刷新金币
	onClickRefresh() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.audioUtil.playSound("Hall/sound/refresh");
		if (!this.refresh) return;
		this.refresh.stopAllActions();
		var _rotateTo = cc.rotateTo(0.2, -180);
		var _rotateTo1 = cc.rotateTo(0.2, -360);
		var seq = cc.sequence(_rotateTo, _rotateTo1);
		this.refresh.runAction(seq);
		this.isClickRefresh = true;
		// this.refreshGlod();
		var _player = cc.vv.session.getPlayer();
		this.setNick(_player.rolename || ``);
		this.lbl_rid.string = "ID:" + _player.rid;
		this.lbl_gold.string = "" + glUtils.getExchangeScore(_player.coin);
	}

	//点击金币
	onClickCoinRim() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		this.openRecharge();
	}

	onClickBack() {
		cc.vv.eventCustom.emit("gameListBack");
	}

	//好友列表;
	onClickFriendList() {
		UIMgr.open(cc.vv.GLConfig.DWC_FriendList_path)
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
	}

	//房间后台;
	onClickRoomManager() {
		UIMgr.open(cc.vv.GLConfig.DWC_RoomManager_path)
		cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
	}



	refreshGlod() {
		return;
		var msg = {
			cmd: "UpdateThirdMoney"
		};
		if (cc.vv.NetGameHall) {
			console.log("_____refresh gold");
			cc.vv.NetGameHall.send("UpdateThirdMoney", msg, function (error, data) {
				cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
				if (!error) {
					//成功
					console.log("______refresh gold Success");
					this.refreshTime = 0;
				} else {
					console.log("______refresh gold faild");
					if (this.isClickRefresh) {
						cc.vv.GLUtils.showTips(data.errcodedes);
					}
				}
				this.isClickRefresh = false;
			}.bind(this)
			);
		}
	}

	//加钱
	onClickAdd() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		this.openRecharge();
	}

	onClickVip() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.vip_path);
	}

	onClickActivity() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.Notice_path);
	}

	onClickRanking() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.ranking_path);
	}

	netSuccess() {
		if (this.refreshTime) {
			this.refreshGlod();
		}
	}

	phoneBindEnd() {
		//	this.gift.active = false;
	}

	openRecharge() {
		let is_bind = cc.vv.GLUtils.checkPhoneBind();
		if (!is_bind) {
			//cc.vv.UIMgr.open(cc.vv.GLConfig.Gift_path);

			cc.vv.UIMgr.open(cc.vv.GLConfig.IdentifyView_path);
			return;
		}
		//cc.vv.GLUtils.showTips("请联系客服或者您的代理上级");
		cc.vv.rechargeConfig.open();
	}
	refreshRedPoint() {

	}
	refreshRedPointNum() {
	}

	changeHeadEnd() {
		this.updatePlayerInfo();
	}

	vipLevelChange() {
		this.updatePlayerInfo();
	}


	//复制官网
	onClickCopy() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		let str = cc.vv.session.getRid()
		cc.vv.GLSDKMgr.setClipboard(str);
		cc.vv.GLUtils.showTips("复制成功");
	}

	//抽奖
	onClickLuckDraw() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.luckDraw_path);
	}

	//邀请有礼
	onClickYaoQing() {
		cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_ExchangeCode_path);
	}

	//修复
	onClickXiuFu() {

	}

	//推广赚金
	onClickTuiGuang() {

	}

	setBack(isShow) {
		cc.vv.Timer.start(0.0, function () {
			this.setTopButton();
		}.bind(this), this.node);
	}

	StartShowGame() {
		this._fpjtable.active = false
	}
	ExitShowGame() {
		this._fpjtable.active = true
	}
	onClickDetailChat() {
		let isHall = true;
		let talkParam = {
			isHall: isHall,
			chatId: cc.vv.session.getParentRid(),
		};
		cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_DetailChat_path, null, null, talkParam);
	}
	//后台管理
	onClickManager() {

		let postData = {};
		postData["cmd"] = "getAgentbgURL";
		cc.vv.GLNet.netPost(postData, (data) => {
			data = JSON.parse(data);
			if (data.err) {
				cc.vv.GLUtils.showTips(data.err);
			} else {
				cc.sys.openURL(data.res);
			}
		});
	}

}
